package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.RestBean;
import com.hs.entity.dto.TeamEmp;
import com.hs.entity.dto.User;
import com.hs.service.TeamEmpService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/teamEmp")
public class TeamEmpController {
    @Resource
    private TeamEmpService teamEmpService;

    @GetMapping("/getTeamEmpList/{currentNum}")
    public RestBean<PageInfo<TeamEmp>> getTeamEmpList(
            @PathVariable("currentNum") Integer currentNum,
            @RequestParam(required = false) String keywords) {
        PageInfo<TeamEmp> teamEmpList = teamEmpService.selectTeamEmpList(currentNum,keywords);
        return RestBean.success(teamEmpList);
    }

    @GetMapping("/getTeamEmpListByTeamId/{currentNum}")
    public RestBean<PageInfo<TeamEmp>> getTeamEmpList(
            @PathVariable("currentNum") Integer currentNum,
            @RequestParam(required = false) String keywords,
            @RequestParam Integer teamId) {
        PageInfo<TeamEmp> teamEmpList = teamEmpService.selectTeamEmpListByTeamId(currentNum,keywords,teamId);
        return RestBean.success(teamEmpList);
    }

    @GetMapping("/getTeamEmpById/{id}")
    public RestBean<List<TeamEmp>> getTeamEmpById(@PathVariable  Integer id) {
        List<TeamEmp> teamAllEmpList = teamEmpService.selectTeamEmpById(id);
        return RestBean.success(teamAllEmpList);
    }

    @PostMapping("/updateTeamEmp")
    public RestBean<TeamEmp> updateTeamEmp( @RequestBody TeamEmp teamEmp) {
        System.out.println(teamEmp);
        teamEmpService.updateById(teamEmp);
        return RestBean.success(null);
    }

    @PostMapping("/addTeamEmp")
    public RestBean<TeamEmp> addTeamEmp( @RequestBody TeamEmp teamEmp) {
        teamEmp.setCreate_time(LocalDateTime.now());
        teamEmpService.save(teamEmp);
        return RestBean.success(null);
    }

    @PostMapping("/deleteTeamEmp/{id}")
    public RestBean<TeamEmp> deleteTeamEmp(@PathVariable  Integer id) {
        teamEmpService.removeById(id);
        return RestBean.success(null);
    }

    @PostMapping("/teamByUsername/{username}")
    public RestBean<TeamEmp> teamByUsername(@PathVariable  String  username) {
        log.info(username);
        TeamEmp loginUser = teamEmpService.teamByUsername(username);
        return RestBean.success(loginUser);
    }
}
